package com.tl.web02.controller.ajax_version;

import com.alibaba.fastjson.JSONObject;
import com.tl.web02.config.Config;
import com.tl.web02.constant.ResponseResult;
import com.tl.web02.service.LoginAndRegisterService;
import com.tl.web02.utils.JsonParamUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "AjaxRegisterServlet", value = "/ajax_register")
public class AjaxRegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        JSONObject jsonParam = JsonParamUtil.getJsonParam(request);
        String username = jsonParam.getString("username");
        String password = jsonParam.getString("password");
        //配置响应编码
        request.setCharacterEncoding(Config.REQ_ChARSET);
        response.setContentType(Config.RESP_JSON_CONTENT_TYPE);
        //获取输出
        PrintWriter out = response.getWriter();
        //获取全局域
        ServletContext application = request.getServletContext();
        //注册的方法
        ResponseResult result = LoginAndRegisterService.register(application, username, password);
        //向页面输出响应
        out.print(JSONObject.toJSONString(result));

    }
}
